package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.R;
import com.example.entity.History2;
import com.example.service.History2Service;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("history2s")
public class History2Controller {

    @Autowired
    private History2Service history2Service;


    /**
     * 浏览记录列表查询
     * @return
     */
    @GetMapping("/getListHistory2")
    public R getListHistory2(){

        List<History2> history2List = history2Service.list();
        return R.success("浏览记录列表查询！",history2List);
    }

    /**
     * 浏览记录列表查询(条件)
     * @return
     */
    @GetMapping("/getListHistory2ByContent")
    public R getListHistory2ByContent(@RequestParam String content2) {

        List<History2> history2List = history2Service.list();
        return R.success("浏览记录搜索查询！", history2List);
    }


    /**
     * 添加浏览记录
     * @return
     */
    @PostMapping("/addHistory2")
    public R addHistory2(@RequestBody History2 history2){



        // 获取当前系统时间
        LocalDateTime currentTime = LocalDateTime.now();

        // 定义日期时间格式化器
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");

        // 将 LocalDateTime 转化为 String
        String formattedTime = currentTime.format(formatter);
        history2.setTime(formattedTime);


        boolean save = history2Service.save(history2);
        if (save){
            return R.success("添加浏览记录成功！",null);
        }else {
            return R.error("添加浏览记录失败！");
        }
    }




    /**
     * 通过id查询浏览记录
     * @return
     */
    @GetMapping("/getHistory2ById")
    public R getHistory2ById(@RequestParam int id) {

        LambdaQueryWrapper<History2> lqw =new LambdaQueryWrapper<>();
        lqw.eq(History2::getId,id);
        History2 one = history2Service.getOne(lqw);
        return R.success("查询浏览记录",one);
    }

    /**
     * 修改浏览记录
     * @return
     */
    @PutMapping("/updateHistory2")
    public R updateHistory2(@RequestBody History2 history2){
        boolean b = history2Service.updateById(history2);
        if (b){
           return R.success("修改浏览记录成功",history2);
        }else {
           return R.error("修改浏览记录失败！");
        }
    }

    /**
     * 通过id删除浏览记录
     * @return
     */
    @DeleteMapping("/deleteHistory2/{id}")
    public R deleteHistory2(@PathVariable int id){

        boolean b = history2Service.removeById(id);
        if (b){
           return R.success("删除浏览记录成功",id);
        }else {
           return R.error("删除浏览记录失败！");
        }
    }



}

